package com.ytjj.qmyx.admin.controller.supplyChain;


import com.alibaba.fastjson.JSONObject;
import com.kuaidi100.sdk.enums.ExpressEnums;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.enums.ExceptionOrderClassifyEnum;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.excel.OrdersExceptionExcel;
import com.ytjj.qmyx.admin.model.request.ExceptionOrdersRequest;
import com.ytjj.qmyx.admin.model.response.ImportExcelFailResponse;
import com.ytjj.qmyx.admin.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 异常订单相关接口
 * @version 1.0
 */
@RestController
@RequestMapping("/exceptionOrders")
@Slf4j
public class ExceptionOrdersController {

    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Resource
    private OrdersService ordersService;

    /**
     * 供应商异常订单跟踪页--查询
     * @param exceptionOrdersRequest
     * @return
     */
    @PostMapping("/getExceptionOrders")
    @OperLog(operModule = "供应链异常订单", operDesc = "列表查询", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getExceptionOrders(@RequestBody ExceptionOrdersRequest exceptionOrdersRequest){
        exceptionOrdersRequest.setRequestType(1);
        String result = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/order/getExceptionOrders",
                JSONObject.toJSONString(exceptionOrdersRequest), supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
        return commonResult;
    }

    /**
     * 供应商异常订单统计
     * @param exceptionOrdersRequest
     * @return
     */
    @PostMapping("/getExceptionOrdersStatistics")
    public CommonResult getExceptionOrdersStatistics(@RequestBody ExceptionOrdersRequest exceptionOrdersRequest){
        exceptionOrdersRequest.setRequestType(1);
        String result = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/order/getExceptionOrdersStatistics",
                JSONObject.toJSONString(exceptionOrdersRequest), supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
        return commonResult;
    }

    /**
     * 获取所有异常值列表
     */
    @GetMapping("/getAllExpressException")
    public CommonResult<Map<String, String>> getAllExpressException(){
        String result = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/order/getAllExpressException", supplyApiConfig.getHeadsMap());
        if(StringUtils.isEmpty(result)){
            throw new ApiException("接口调用出错！");
        }
        CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
        return commonResult;
    }

    /**
     * 供应链异常订单跟踪页--导出异常订单
     * @param exceptionOrdersRequest
     * @return
     */
    @PostMapping("/exportExceptionOrders")
    @OperLog(operModule = "供应链异常订单", operDesc = "列表导出", operType = SysLogConstants.OPERATION_DOWNLOAD)
    public void batchExportExceptionOrders(HttpServletResponse response, @RequestBody ExceptionOrdersRequest exceptionOrdersRequest) {
        ordersService.exportExceptionOrders(response,exceptionOrdersRequest);
    }


    /**
     * 查询异常处理归类
     * @return
     */
    @GetMapping("/getHandleClassify")
    public CommonResult<List<Map<Integer,String>>> getHandleClassify() {
        List<Map<Integer,String>> exceptionOrderClassify = ExceptionOrderClassifyEnum.getAllExceptionClassify();
        return  CommonResult.success(exceptionOrderClassify);
    }

    /**
     * 批量供应商异常订单跟踪页--导入excel批量处理
     * @return
     */
    @PostMapping("/batchInsertNote")
    public CommonResult<List<ImportExcelFailResponse>> batchInsertNote(MultipartFile file) throws IOException {
        return ordersService.batchInsertNote(file);
    }

    /**
     * 批量归类异常订单
     * @param ids 逗号分割的 id
     * @param type 异常归类 key
     * @return CommonResult
     */
    @PostMapping("/batchDealExceptionOrders")
    public CommonResult batchDealExceptionOrders(Integer[] ids, Integer type) throws IOException {
        return ordersService.batchDealExceptionOrders(ids,type);
    }

    /**
     * 异常单设置成已完结
     * @return
     */
    @PostMapping("/showExceptionOrders")
    public CommonResult<List<ImportExcelFailResponse>> showExceptionOrders(@RequestBody ExceptionOrdersRequest exceptionOrdersRequest) throws IOException {
        return ordersService.showExceptionOrders(exceptionOrdersRequest);
    }
}
